package tcc.board;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class BoardConnection {
	private Socket mSocket;
	private PrintWriter mTX;
	private BufferedReader mRX;
	
	private static BoardConnection mInstance;
	
	private BoardConnection() {
	}
	
	public static BoardConnection getInstance() {
		if (mInstance == null)
			mInstance = new BoardConnection();
		return mInstance;
	}

	public void connect(String ip, String port) throws UnknownHostException, IOException {
		if (mSocket != null)
			mSocket.close();
    	mSocket = new Socket(ip, Integer.parseInt(port));

		mTX = new PrintWriter(mSocket.getOutputStream(), true);
		mRX = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
	}
		
	public String request(String request) throws IOException {
		mTX.println(request);
		return mRX.readLine();
	}
}
